<template>
  <container-item-wrapper :widget="widget">
    <el-container class="card-container" :class="[!!widget.options.folded ? 'folded' : '', customClass]"
             :shadow="widget.options.shadow" :style="{width: widget.options.cardWidth + '!important' || ''}"
             :ref="widget.id" v-show="!widget.options.hidden">
      <el-form-item label-width="0px">
        <template #default>
          <span class="suffix left">{{widget.options.preSuffix}}</span>
          <component :is="dynamicComponent" :options="widget.options"></component>
        </template>
      </el-form-item>
    </el-container>
  </container-item-wrapper>
</template>

<script>
  import emitter from '@/utils/emitter'
  import i18n from "@/utils/i18n"
  import refMixin from "@/components/form-render/refMixin"
  import ContainerItemWrapper from '@/components/form-render/container-item/container-item-wrapper'
  import containerItemMixin from "@/components/form-render/container-item/containerItemMixin"
  import {shallowRef} from "vue";

  export default {
    name: "fy-item",
    componentName: 'FyItem',
    mixins: [emitter, i18n, refMixin, containerItemMixin],
    components: {
      ContainerItemWrapper,
      'placeholder': {
        // 初始化一个占位组件的模板
        template: '<p>Loading...</p>'
      }
    },
    data() {
      return {
        // 初始化为一个占位组件
        dynamicComponent: 'placeholder'
      };
    },
    props: {
      widget: Object,

      subFormRowIndex: { /* 子表单组件行索引，从0开始计数 */
        type: Number,
        default: -1
      },
      subFormColIndex: { /* 子表单组件列索引，从0开始计数 */
        type: Number,
        default: -1
      },
      subFormRowId: { /* 子表单组件行Id，唯一id且不可变 */
        type: String,
        default: ''
      },
    },
    computed: {
      customClass() {
        return this.widget.options.customClass || ''
      },

    },
    created() {
      let renderComponentName = this.widget.option.renderComponentName
      if (renderComponentName){
        import(renderComponentName+".vue").then((component) => {
          // 导入成功后，将组件设置为导入的组件
          this.dynamicComponent = shallowRef(component.default);
        }).catch(error => {
          // 处理导入组件时的错误
          console.error('Dynamic component loading failed:', error);
        });
      }
    },
    methods: {
    },
  }
</script>

<style lang="scss" scoped>
  :deep(.el-card__header) {
    padding: 10px 12px;
  }

  .folded :deep(.el-card__body) {
    display: none;
  }

  .clear-fix:before, .clear-fix:after {
    display: table;
    content: "";
  }

  .clear-fix:after {
    clear: both;
  }

  .float-right {
    float: right;
  }
  .block{
    width: 100%;
    height: 100px;
    text-align: center;
    line-height: 100px;
    background-color: #327edb;
    color: white;
    font-size: 24px;
  }
</style>
